// Triple Oscillator in Faust - 修复版本
// Based on LMMS TripleOscillator plugin

import("stdfaust.lib");

// 波形选择器
waveShape(waveType, freq, phase) = 
    selectn(waveType, 7,
        os.osc(phase),                           // 0: Sine
        os.triangle(phase),                      // 1: Triangle
        os.sawtooth(phase),                      // 2: Sawtooth
        os.square(phase),                        // 3: Square
        os.sawtooth(phase) : fi.lowpass(4, 2000/ma.SR) : *(0.7), // 4: Moog-like saw approximation
        os.phasor(freq) : *(2) : -(1) : max(0) : *(2) : -(1),   // 5: Exponential approximation
        no.noise                                // 6: White noise
    );

// 单个振荡器模型
oscillator(waveType, freq, detune, phaseOffset, volume, pan) =
    waveShape(waveType, freq, os.phasor(freq * detune) + phaseOffset)
    : *(volume)
    <: @(ma.SR * 0.5 * (pan - 1)), @(ma.SR * 0.5 * (pan + 1));

// 调制函数
modulate(modType, carrier, modulator) =
    selectn(modType, 5,
        carrier + modulator : min(1) : max(-1),     // 0: Phase Modulation
        carrier * (1 + modulator),                  // 1: Amplitude Modulation
        (carrier + modulator)/2,                    // 2: Mix
        carrier,                                    // 3: Sync (简化实现)
        carrier * (1 + modulator/2)                 // 4: Frequency Modulation approximation
    );

// 控制参数
// 振荡器1控制参数
vol1 = hslider("Osc1/Volume", 0.33, 0, 1, 0.01);
pan1 = hslider("Osc1/Panning", 0, -1, 1, 0.01);
coarse1 = hslider("Osc1/Coarse[-24,24]", 0, -24, 24, 1);
fine1 = hslider("Osc1/Fine[-100,100]", 0, -100, 100, 1);
phase1 = hslider("Osc1/Phase[0,1]", 0, 0, 1, 0.01);
wave1 = hslider("Osc1/Waveform[0-6]", 0, 0, 6, 1);

// 振荡器2控制参数
vol2 = hslider("Osc2/Volume", 0.33, 0, 1, 0.01);
pan2 = hslider("Osc2/Panning", 0, -1, 1, 0.01);
coarse2 = hslider("Osc2/Coarse[-24,24]", 0, -24, 24, 1);
fine2 = hslider("Osc2/Fine[-100,100]", 0, -100, 100, 1);
phase2 = hslider("Osc2/Phase[0,1]", 0, 0, 1, 0.01);
wave2 = hslider("Osc2/Waveform[0-6]", 0, 0, 6, 1);

// 振荡器3控制参数
vol3 = hslider("Osc3/Volume", 0.33, 0, 1, 0.01);
pan3 = hslider("Osc3/Panning", 0, -1, 1, 0.01);
coarse3 = hslider("Osc3/Coarse[-24,24]", 0, -24, 24, 1);
fine3 = hslider("Osc3/Fine[-100,100]", 0, -100, 100, 1);
phase3 = hslider("Osc3/Phase[0,1]", 0, 0, 1, 0.01);
wave3 = hslider("Osc3/Waveform[0-6]", 0, 0, 6, 1);

// 调制类型
mod1_type = hslider("Mod/Osc1 Type[0-4]", 2, 0, 4, 1); // 默认为mix
mod2_type = hslider("Mod/Osc2 Type[0-4]", 2, 0, 4, 1); // 默认为mix

// 频率计算辅助函数
// 将半音和cents转换为频率倍数
detuneRatio(coarse, fine) = 2^(coarse/12 + fine/1200);

// 三振荡器合成器主体
process = pm TripleOscillator;

// 带MIDI输入的三振荡器
TripleOscillator = 
    midiIn : poly(midiIn, 1, triOscVoice, 8) : *(0.33) <: _, _
    with {
        triOscVoice(freq, gain, gate, mod) = 
            let {
                // 计算每个振荡器的频率
                freq1 = freq * detuneRatio(coarse1, fine1);
                freq2 = freq * detuneRatio(coarse2, fine2);
                freq3 = freq * detuneRatio(coarse3, fine3);
                
                // 生成三个振荡器信号
                osc1_signal = oscillator(int(wave1), freq1, 1.0, phase1, vol1, pan1);
                osc2_signal = oscillator(int(wave2), freq2, 1.0, phase2, vol2, pan2);
                osc3_signal = oscillator(int(wave3), freq3, 1.0, phase3, vol3, pan3);
                
                // 应用调制
                modulated_osc2_1 = modulate(int(mod1_type), osc1_signal, osc2_signal);
                final_signal = modulate(int(mod2_type), modulated_osc2_1, osc3_signal);
                
                // 应用包络和增益
                envelope = gate : en.adsre(0.01, 0.1, 0.8, 0.2);
                
                // 输出最终信号
                final_signal * envelope * gain;
            };
    };